//
//  MagazineViewController.m
//  ChineseCollegeStudentsNetwork
//
//  Created by 刘喆 on 2018/5/9.
//  Copyright © 2018年 刘喆. All rights reserved.
//

#import "MagazineViewController.h"
#import "BannerView.h"
#import "MagazineTableViewCell.h"
#import "CommonModel.h"
#import "MLAWebVC.h"
#import "NovelViewController.h"

static NSString *const MagazineTableViewCell_ID = @"MagazineTableViewCell_ID";


@interface MagazineViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) SDCycleScrollView *headView;
@property (nonatomic, strong) NSMutableArray *listArr;
@property (nonatomic, copy) NSArray *bannersArr;

@end

@implementation MagazineViewController

- (SDCycleScrollView *)headView {
    if(!_headView){
        _headView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.view.frame.size.width/1.875) delegate:self placeholderImage:[UIImage imageNamed:@"飞艇"]];
        _headView.clickItemOperationBlock = ^(NSInteger currentIndex) {
            CommonModel *model = _bannersArr[currentIndex];
            MLAWebVC *webVC = [[MLAWebVC alloc] init];
            webVC.urlStr = model.navigationUrl;
            webVC.titleStr = model.title;
            [self.navigationController pushViewController:webVC animated:YES];
        };
    }
    return _headView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupConstraints];

    self.headView.backgroundColor = [UIColor groupTableViewBackgroundColor];
   
    
    [MLAHttpManage postHttpType:MLAHttpRequestType_Get hostType:MLAHttpHostType_Api api:@"http://cms.chinacampus.org/api/v1/stl/sqlContents?apiKey=6de9cc05-6c46-4e29-84f3-a638e442465a&queryString=select%20*%20from%20ss_magazine%20where%20channelId%20=%2041%20order%20by%20taxis%20desc" params:nil success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        _listArr = [NSMutableArray array];
        _listArr = [CommonModel mj_objectArrayWithKeyValuesArray:response[@"value"]];
        
        [self.tableView reloadData];

    } failure:^{
        
    }];
    
    [MLAHttpManage postHttpType:MLAHttpRequestType_Get hostType:MLAHttpHostType_Api api:@"/api/v1/stl/contents" params:@{@"channelName":@"首页Banner"} success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        _bannersArr = [CommonModel mj_objectArrayWithKeyValuesArray:[(NSDictionary *)response objectForKey:@"value"]];
        NSMutableArray *listArr = [NSMutableArray array];
        for (CommonModel *model in _bannersArr) {
            [listArr addObject:model.imageUrl];
        }
        _headView.imageURLStringsGroup = listArr;
        
    } failure:^{
        
    }];

}

- (void)setupConstraints {
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset = 0;
    }];
    
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.backgroundColor = UIColor.whiteColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[MagazineTableViewCell class] forCellReuseIdentifier:MagazineTableViewCell_ID];
         _tableView.tableHeaderView = self.headView;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        [self.view addSubview:self.tableView];
    }
    return _tableView;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return _listArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MagazineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MagazineTableViewCell_ID forIndexPath:indexPath];
    CommonModel *model = _listArr[indexPath.section];
    [cell.leftimg sd_setImageWithURL:[NSURL URLWithString:model.imageUrl] placeholderImage:[UIImage imageNamed:@"飞艇"]];
    cell.titleL.text = model.title;
    cell.timeL.text = [NSString stringWithFormat:@"上架时间：%@",model.addDate];
    cell.freeReadingnumL.text = [NSString stringWithFormat:@"免费阅读篇数：%@篇",model.freeCount];
    cell.payReadingnumL.text = [NSString stringWithFormat:@"付费阅读篇数：%@篇",model.payCount];
    cell.purchaseL.text = [NSString stringWithFormat:@"纸质购买价格：%@元",model.paperCost];
    cell.ReadL.text = [NSString stringWithFormat:@"全刊电子阅读价格：%@元",model.readingCost];

    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 180;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return section == 1 ? 0.001 : 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return section == 3 ? 0.0001 : 20;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NovelViewController *novel = [[NovelViewController alloc] init];
    novel.curModel = _listArr[indexPath.section];
    [self.navigationController pushViewController:novel animated:YES];

}

@end
